package com.hrt.manage.cash.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.Cash;
import com.hrt.manage.cash.dao.CashDao;
import com.hrt.manage.cash.service.CashService;

@Service
public class CashServiceImpl implements CashService {

	@Autowired
	private CashDao dao;

	@Override
	@Transactional
	public void saveCash(Cash cash) {
		// TODO Auto-generated method stub
		dao.saveEntity(cash);
	}

	@Override
	public List<Cash> queryByPage(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		return dao.queryByPage(pageIndex, pageSize);
	}

	@Override
	public Long[] deleteById(Long[] cashIds) {
		// TODO Auto-generated method stub
		List<Long> delArr=new ArrayList<Long>();
		if(cashIds!=null && cashIds.length>0){
			for(Long id:cashIds){
			Cash c=dao.getById(id);
			c.setIsdelete(new Integer(1));
			dao.updateEntity(c);
			delArr.add(id);
			}
		}
		return (Long[]) delArr.toArray();
	}

	@Override
	public Cash getCashByid(Long cashId) {
		
		return dao.getCashByid(cashId);
	}

	@Override
	@Transactional
	public void udateCash(Cash cash) {
		// TODO Auto-generated method stub
		dao.updateEntity(cash);
	}

	@Override
	public Long totalCount(Map<String,Object> condition) {
		// TODO Auto-generated method stub
		
		return dao.totalCount(condition);
	}

	@Override
	public Timestamp getDate() {
		// TODO Auto-generated method stub
		return dao.getDate();
	}

	@Override
	public List<Cash> queryByPage(List<Map<String, Object>> conditions,
			Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
		String sql="select c from Cash c where c.isdelete=0 ";
		
		if(conditions!=null && !conditions.isEmpty()){
		for(Map condition:conditions){
			if(condition!=null && !condition.isEmpty()){
				for(Iterator<Entry> itr=condition.entrySet().iterator();itr.hasNext();){
					Entry entry=itr.next();
					String fieldName=entry.getKey().toString();
					String value=entry.getValue().toString();
					
					sql=sql+fieldName+value;
				}
				
			}
			sql=sql+" order by c.createTime desc";
		}
		}
		System.out.println("----------------->"+sql);
		return dao.queryByPage(sql, pageIndex, pageSize);
	}
	
	

}
